//index.js
const news= require('../../config').news;
const getCurrentPagesApi = require('../../utils/common').getCurrentPagesApi;
const host = require('../../config').host
//获取应用实例
const app = getApp()

var page = 0;
var end = true;
// 获取数据的方法，具体怎么获取列表数据大家自行发挥  
var GetList = function(that){  
  that.setData({  
    hidden:false  
  });
  end = false;
  console.log(app.globalData.typeId)
  wx.request({  
    url:host+'api/english/newslists',  
    data:{  
      page : page,
    },  
    success:function(res){
      // console.info(end);
      
      if(res.data != ""){
        var list = that.data.news;
        for(var i = 0; i < res.data.length; i++){
          list.push(res.data[i]);
        }
        that.setData({  
          news : list  
        });  
        page ++;
        end = true;
        that.setData({  
          hidden:true  
        });
      }else{
        end = false
        that.setData({  
          hiddens:true,
          hidden:true
        });
      }
    }  
  });  
}


Page({
  data: {
    news: [],
    currentNewsId: 1,
    scrollHeight: 0,
    showModal:true,
    hidden:true,
    hiddens:false,
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  news_detail: function(e) {
    wx.navigateTo({
      url: `./news_detail?id=${e.currentTarget.dataset.id}`
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return {
        title: 'NEWS INFORMATION',
        path: getCurrentPagesApi()
      }
    }
    
  },
  onLoad: function () {
    app.globalData.typeId = 1;
    this.refresh(this);
    var that = this;
    wx.getSystemInfo({
       success:function(res){
         // console.info(res.windowHeight);  
         that.setData({
           scrollHeight:res.windowHeight
         });
       }
   });
    
  },
  bindDownLoad:function(){  
    //  该方法绑定了页面滑动到底部的事件  
     var that = this;
     console.log(end);
     if(end){
      GetList(that);  
     }
   },
   refresh:function(event){  
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新  
     page = 0;  
     this.setData({  
       news : [],  
       scrollTop : 0  
     });  
     GetList(this)  
   },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
